<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php 
//
include_once("..\\common.php");
$admin = false; 
CheckSalerLogon();
?>
      
 <?php

$salerid = $_SESSION["salerid"];
$clientid = $_GET["clientid"];

$posts = $_POST; 

foreach ($posts as $key => $value) {
	$posts[$key] = trim($value); 
} 
$DB = DBConnect();

$othersalerid = $posts["othersaler"];
echo $othersalerid;

$bkeeptrack = isset($posts["keeptrackon"])?1:0;
$submit_time = date("Y-m-d H:i:s");


//first check if the client in other saler's list
$query = "SELECT id FROM client_saler_rel WHERE salerid = '$othersalerid' AND clientid =$clientid AND state ='active'  ";
$result = mysql_query($query,$DB);
if(mysql_fetch_row($result))
{
	echo "其他销售人员已有此客户";
	die("");
}


mysql_query("START TRANSACTION",$DB);
$result1=1;
if($bkeeptrack ==0) //remove form list
{
	$query = "UPDATE client_saler_rel SET state='closed',endtime='$submit_time' WHERE clientid='$clientid' AND salerid = '$salerid' ";
	//echo $query;
	$result1 = mysql_query($query,$DB);	
}

$query ="INSERT INTO client_saler_rel SET salerid = '$othersalerid', clientid ='$clientid', starttime='$submit_time',  state='active' ";
$result2 = mysql_query($query,$DB);

if ( $result1 && $result2 )
	{
		echo "转换成功!";	
		mysql_query("COMMIT",$DB);
		
	}
	else 
	{
		mysql_query("ROLLBACK",$DB);
		die("转换失败!");	
	}

echo "<a href= 'index.php'>回主页</a> ";
?>

</body>
</html>